package fxana;

import java.util.Date;
import java.util.Vector;

public class Range {
	static public final int TYPE_NONE=0;
	static public final int TYPE_NUMBER=1;
	static public final int TYPE_DATE=2;
	
	protected Double number1=null,number2=null;
	protected Date date1=null,date2=null;
	protected Integer type=Range.TYPE_NONE;
	
	public Range(Integer a,Integer b){
		if(a<=b){
			number1=a.doubleValue();
			number2=b.doubleValue();
		}else{
			number1=b.doubleValue();
			number2=a.doubleValue();
		}
		
		this.type=Range.TYPE_NUMBER;
	}
	
	public Range(Float a,Float b){
		if(a.compareTo(b)<=0){
			number1=a.doubleValue();
			number2=b.doubleValue();
		}else{
			number1=b.doubleValue();
			number2=a.doubleValue();
		}
		
		this.type=Range.TYPE_NUMBER;
	}
	
	public Range(Double a,Double b){
		if(a.compareTo(b)<=0){
			number1=a.doubleValue();
			number2=b.doubleValue();
		}else{
			number1=b.doubleValue();
			number2=a.doubleValue();
		}
		
		this.type=Range.TYPE_NUMBER;
	}
	
	public Range(Date a,Date b){
		if(a==null || b==null)
			return;
		if(a.getTime()<=b.getTime()){
			date1=a;
			date2=b;
		}else{
			date1=b;
			date2=a;
		}
		
		this.type=Range.TYPE_DATE;
	}
	
	public boolean InRange(Integer x){
		if(this.type!=Range.TYPE_NUMBER)
			return false;
		return ((number1.compareTo(x.doubleValue())<=0 && (number2.compareTo(x.doubleValue())>0) )||
				((number1.compareTo(x.doubleValue())<0 && (number2.compareTo(x.doubleValue())>=0))));
	}
	
	public boolean InRange(Float x){
		if(this.type!=Range.TYPE_NUMBER)
			return false;
		return ((number1.compareTo(x.doubleValue())<=0 && (number2.compareTo(x.doubleValue())>0) )||
				((number1.compareTo(x.doubleValue())<0 && (number2.compareTo(x.doubleValue())>=0))));
	}
	
	public boolean InRange(Double x){
		if(this.type!=Range.TYPE_NUMBER)
			return false;
		return ((number1.compareTo(x)<=0 && (number2.compareTo(x)>0) )||
				((number1.compareTo(x)<0 && (number2.compareTo(x)>=0))));
	}
	
	public boolean InRange(Date x){
		if(this.type!=Range.TYPE_DATE)
			return false;
		return (date1.before(x)) && date2.after(x) || (date1==x) || (date2==x);
	}
	
	public Integer GetType(){
		return this.type;
	}
	
	public Vector<Double> GetNumberRange(){
		if(this.type!=Range.TYPE_NUMBER)
			return null;
		
		Vector<Double> result=new Vector<Double>();
		result.add(number1);
		result.add(number2);
		return result;
	}
	
	public Vector<Date> GetDateRange(){
		if(this.type!=Range.TYPE_DATE)
			return null;
		
		Vector<Date> result=new Vector<Date>();
		result.add(this.date1);
		result.add(this.date2);
		return result;
	}
	
	public String GetRangeString(){
		switch(this.type){
		case Range.TYPE_NUMBER:
			return "["+this.number1+","+this.number2+"]";
		case Range.TYPE_DATE:
			return "<"+DateCell.DateToString(date1)+" - "+DateCell.DateToString(date2)+">";
		default:
			return null;
		}
	}
}
